const express = require('express');
const router = express.Router();
const neo4jService = require('../services/neo4jService');

// 获取图谱数据
router.get('/data', async (req, res) => {
  try {
    const graphData = await neo4jService.getGraphData();
    res.json({
      success: true,
      data: graphData
    });
  } catch (error) {
    console.error('获取图谱数据失败:', error);
    res.status(500).json({
      success: false,
      error: '获取图谱数据失败',
      message: error.message
    });
  }
});

// 获取所有节点
router.get('/nodes', async (req, res) => {
  try {
    const nodes = await neo4jService.getAllNodes();
    res.json({
      success: true,
      data: nodes
    });
  } catch (error) {
    console.error('获取节点失败:', error);
    res.status(500).json({
      success: false,
      error: '获取节点失败',
      message: error.message
    });
  }
});

// 创建节点
router.post('/nodes', async (req, res) => {
  try {
    const { label, properties } = req.body;
    
    if (!label || !properties) {
      return res.status(400).json({
        success: false,
        error: '缺少必要参数',
        message: '需要提供label和properties'
      });
    }
    
    const result = await neo4jService.createNode(label, properties);
    res.json({
      success: true,
      data: result,
      message: '节点创建成功'
    });
  } catch (error) {
    console.error('创建节点失败:', error);
    res.status(500).json({
      success: false,
      error: '创建节点失败',
      message: error.message
    });
  }
});

// 创建关系
router.post('/relationships', async (req, res) => {
  try {
    const { fromNodeId, toNodeId, relationshipType, properties } = req.body;
    
    if (!fromNodeId || !toNodeId || !relationshipType) {
      return res.status(400).json({
        success: false,
        error: '缺少必要参数',
        message: '需要提供fromNodeId、toNodeId和relationshipType'
      });
    }
    
    const result = await neo4jService.createRelationship(
      parseInt(fromNodeId),
      parseInt(toNodeId),
      relationshipType,
      properties || {}
    );
    
    res.json({
      success: true,
      data: result,
      message: '关系创建成功'
    });
  } catch (error) {
    console.error('创建关系失败:', error);
    res.status(500).json({
      success: false,
      error: '创建关系失败',
      message: error.message
    });
  }
});

// 搜索节点
router.get('/search', async (req, res) => {
  try {
    const { q } = req.query;
    
    if (!q) {
      return res.status(400).json({
        success: false,
        error: '缺少搜索参数',
        message: '需要提供搜索关键词q'
      });
    }
    
    const results = await neo4jService.searchNodes(q);
    res.json({
      success: true,
      data: results
    });
  } catch (error) {
    console.error('搜索失败:', error);
    res.status(500).json({
      success: false,
      error: '搜索失败',
      message: error.message
    });
  }
});

// 删除节点
router.delete('/nodes/:id', async (req, res) => {
  try {
    const nodeId = parseInt(req.params.id);
    
    if (isNaN(nodeId)) {
      return res.status(400).json({
        success: false,
        error: '无效的节点ID',
        message: '节点ID必须是数字'
      });
    }
    
    await neo4jService.deleteNode(nodeId);
    res.json({
      success: true,
      message: '节点删除成功'
    });
  } catch (error) {
    console.error('删除节点失败:', error);
    res.status(500).json({
      success: false,
      error: '删除节点失败',
      message: error.message
    });
  }
});

// 初始化示例数据
router.post('/init-sample-data', async (req, res) => {
  try {
    await neo4jService.initSampleData();
    res.json({
      success: true,
      message: '示例数据初始化成功'
    });
  } catch (error) {
    console.error('初始化示例数据失败:', error);
    res.status(500).json({
      success: false,
      error: '初始化示例数据失败',
      message: error.message
    });
  }
});

// 执行自定义Cypher查询
router.post('/query', async (req, res) => {
  try {
    const { cypher, parameters } = req.body;
    
    if (!cypher) {
      return res.status(400).json({
        success: false,
        error: '缺少查询语句',
        message: '需要提供cypher查询语句'
      });
    }
    
    const result = await neo4jService.runQuery(cypher, parameters || {});
    res.json({
      success: true,
      data: result
    });
  } catch (error) {
    console.error('执行查询失败:', error);
    res.status(500).json({
      success: false,
      error: '执行查询失败',
      message: error.message
    });
  }
});

module.exports = router;